package net.contrend.system.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import org.springframework.jdbc.core.JdbcTemplate;

import net.contrend.system.form.DeptForm;
import net.contrend.system.mapper.DeptFormMapper;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

public class DeptDao extends BaseDao{
    
    private String table = "ct_department";

    public DeptDao() {
    }
    
    public DeptDao(JdbcTemplate jt) {
        this.jt = jt;
    }
    
    public int insert(DeptForm dept) {
        String columns = "deptid, deptcode, deptname, deptabbr, deptchief, parentid, status, orderno, remark";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(dept.getDeptId())) {
            dept.setDeptId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
                    dept.getDeptId(),
                    dept.getDeptCode(),
                    dept.getDeptName(),
                    dept.getDeptAbbr(),
                    dept.getDeptChief(),
                    dept.getParentId(),
                    dept.getStatus(),
                    dept.getOrderNo(),
                    dept.getRemark()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR
                });
    }
    
    public int update(DeptForm dept) {
        String columns = "deptcode, deptname, deptabbr, deptchief, parentid, status, orderno, remark";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "deptid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        
        return this.jt.update(sql,
                new Object[]{
                    dept.getDeptCode(),
                    dept.getDeptName(),
                    dept.getDeptAbbr(),
                    dept.getDeptChief(),
                    dept.getParentId(),
                    dept.getStatus(),
                    dept.getOrderNo(),
                    dept.getRemark(),
                    dept.getDeptId()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR,
                    Types.VARCHAR
                });
    }
    
    public int delete(String id) {
        String sql = SqlUtils.getDeleteSql(this.table, "deptid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{id}, new int[]{Types.VARCHAR});
    }
    
    @SuppressWarnings("unchecked")
    public DeptForm query(String id) {
        String sql = "select * from ct_department where deptid = ?";
        List<DeptForm> deptList = this.jt.query(sql,
                new Object[]{id}, new int[]{Types.VARCHAR}, new DeptFormMapper());
        if (null != deptList && 0 < deptList.size()) {
            return deptList.get(0);
        }
        return null;
    }
    
}
